<?php
/**
 * @file americandefine.php
 * @author LinLin
 * @date 2016年3月28日 上午10:05:11
 * @brief 
 */
namespace haibao\cms\business\americandrama;

use \haibao\cms\data\Tag as TagData;
use \haibao\cms\data\americandrama\AmericanDefine as AmericanDefineData;
use haibao\cms\model\filter\americandrama\AmericanPlayWeek as AmericanPlayWeekFilter;
use haibao\cms\model\filter\americandrama\AmericanDefine as AmericanDefineFilter;
use haibao\cms\model\filter\americandrama\AmericanStars as AmericanStarsFilter;
use haibao\cms\model\filter\americandrama\AmericanTag as AmericanTagFilter;
use haibao\cms\model\filter\americandrama\AmericanRadio as AmericanRadioFilter;
use haibao\cms\model\filter\americandrama\AmericanCategory as AmericanCategoryFilter;
use haibao\cms\business\americandrama\AmericanDefine as AmericanDefineBusiness;
use \haibao\cms\common\ChineseSpell;
use \haibao\cms\business\BusinessException;
use haibao\frame\data\query\Condition;

class AmericanDefine extends \haibao\cms\business\Base{
    
    public static $meiju_dt_arr = array(
        1=>'CBS',2=>'NBC',3=>'FOX',4=>'TNT',
        5=>'HBO',6=>'AMC',7=>'ABC',8=>'BBC',
        9=>'Showtime',10=>'Starz',11=>'CW',12=>'Netflix',
        13=>'ITV',14=>'Showcase',15=>'其他'
        
    );
    
    public static $meiju_jmtype_arr = array(
        1=>'爱情',2=>'喜剧',3=>'科幻',4=>'悬疑',
        5=>'警匪',6=>'魔幻',7=>'历史',8=>'青春',
        9=>'都市',10=>'家庭',11=>'动作',12=>'律政',
        13=>'惊悚',14=>'战争',15=>'剧情',16=>'传记',
        17=>'罪案',18=>'冒险',19=>'谍战',20=>'综艺',
        21=>'黑帮',22=>'奇幻',23=>'恐怖',24=>'文艺',
        25=>'其他'
    );
    
    
    public static $meiju_date_arr = array(
        1=>'2017',
        2=>'2016',3=>'2015',4=>'2014',5=>'2013',
        6=>'2012',7=>'2011',8=>'2010',9=>'2009',
        10=>'2008',11=>'2007',12=>'2006',13=>'2005',
        14=>'2004',15=>'2003',16=>'2002',17=>'2001',
        18=>'2000',19=>'90年代',20=>'80年代',21=>'70年代',
        22=>'60年代'
    );
    
    
    public static function getAmericanIdByTagId($celebrityTagIdArr){
        $data = new AmericanDefineData();
        return $data->getAmericanIdByTagId($celebrityTagIdArr);
    }
    
    /**
     * 美剧库列表
     * @param unknown $query
     */
    public static function getAmericanDramaLists($query){
        $data = new AmericanDefineData();
        return $data->getAmericanDramaLists($query);
    }
    
    /**
     * 通过美剧ID获取信息
     */
    public static function getAmericanDefineInfoById($americanId){
        $returnOne = true;
        if(is_array($americanId)){
            $returnOne = false;
        }
        $americanDefineFilter = new AmericanDefineFilter();
        $americanDefineFilter->select(array(
            AmericanDefineFilter::CONDITION_FIELD_NAME_ID,
            AmericanDefineFilter::CONDITION_FIELD_NAME_NAMECN,
            AmericanDefineFilter::CONDITION_FIELD_NAME_NAMEEN,
            AmericanDefineFilter::CONDITION_FIELD_NAME_OTHERNAME,
            AmericanDefineFilter::CONDITION_FIELD_NAME_AVATARURL,
            AmericanDefineFilter::CONDITION_FIELD_NAME_COVERURL,
            AmericanDefineFilter::CONDITION_FIELD_NAME_RELEASEDATE,
            AmericanDefineFilter::CONDITION_FIELD_NAME_EPISODESNUM,
            AmericanDefineFilter::CONDITION_FIELD_NAME_CONTENT,
            AmericanDefineFilter::CONDITION_FIELD_NAME_RETURNDATE,
            AmericanDefineFilter::CONDITION_FIELD_NAME_ENDDATE,
            AmericanDefineFilter::CONDITION_FIELD_NAME_PUBLISHED,
            AmericanDefineFilter::CONDITION_FIELD_NAME_PINYINCH,
            AmericanDefineFilter::CONDITION_FIELD_NAME_PINYINEN,
            AmericanDefineFilter::CONDITION_FIELD_NAME_CREATEBY,
            AmericanDefineFilter::CONDITION_FIELD_NAME_CREATETIME,
        ));

        $americanStarFilter = new AmericanStarsFilter();
        $americanStarFilter->select(array(
            AmericanStarsFilter::CONDITION_FIELD_NAME_ID,
            AmericanStarsFilter::CONDITION_FIELD_NAME_AMERICANID,
            AmericanStarsFilter::CONDITION_FIELD_NAME_NAMECH,
            AmericanStarsFilter::CONDITION_FIELD_NAME_NAMEEN,
            AmericanStarsFilter::CONDITION_FIELD_NAME_ACTORNAME,
            AmericanStarsFilter::CONDITION_FIELD_NAME_STARTYPE,
            AmericanStarsFilter::CONDITION_FIELD_NAME_TAGID,
            AmericanStarsFilter::CONDITION_FIELD_NAME_TAGTYPE,
        ));
        $americanTagFilter = new AmericanTagFilter();
        $americanTagFilter->select(array(
            AmericanTagFilter::CONDITION_FIELD_NAME_ID,
            AmericanTagFilter::CONDITION_FIELD_NAME_AMERICANID,
            AmericanTagFilter::CONDITION_FIELD_NAME_TAGID,
        ));

        $americanRadioFilter = new AmericanRadioFilter();
        $americanRadioFilter->select(array(
            AmericanRadioFilter::CONDITION_FIELD_NAME_ID,
            AmericanRadioFilter::CONDITION_FIELD_NAME_AMERICANID,
            AmericanRadioFilter::CONDITION_FIELD_NAME_RADIOID,
        ));

        $americanCategoryFilter = new AmericanCategoryFilter();
        $americanCategoryFilter->select(array(
            AmericanCategoryFilter::CONDITION_FIELD_NAME_ID,
            AmericanCategoryFilter::CONDITION_FIELD_NAME_AMERICANID,
            AmericanCategoryFilter::CONDITION_FIELD_NAME_CATEGORYID,
        ));

        if($returnOne){
            $americanDefineFilter->where(AmericanDefineFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_EQUAL, $americanId);
        }else{
            $americanDefineFilter->where(AmericanDefineFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_IN, $americanId);
        }

        $americanDefineFilter->where(AmericanDefineFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\americandrama\AmericanDefine::STATUS_ENABLE);
        $americanDefineFilter->leftJoin($americanStarFilter,null,AmericanDefineFilter::CONDITION_FIELD_NAME_ID,AmericanStarsFilter::CONDITION_FIELD_NAME_AMERICANID,AmericanDefineFilter::CONDITION_FIELD_NAME_AMERICANSTARS);
        $americanDefineFilter->leftJoin($americanTagFilter,null,AmericanDefineFilter::CONDITION_FIELD_NAME_ID,AmericanTagFilter::CONDITION_FIELD_NAME_AMERICANID,AmericanDefineFilter::CONDITION_FIELD_NAME_AMERICANTAG);
        $americanDefineFilter->leftJoin($americanRadioFilter,null,AmericanDefineFilter::CONDITION_FIELD_NAME_ID,AmericanRadioFilter::CONDITION_FIELD_NAME_AMERICANID,AmericanDefineFilter::CONDITION_FIELD_NAME_AMERICANRADIO);
        $americanDefineFilter->leftJoin($americanCategoryFilter,null,AmericanDefineFilter::CONDITION_FIELD_NAME_ID,AmericanCategoryFilter::CONDITION_FIELD_NAME_AMERICANID,AmericanDefineFilter::CONDITION_FIELD_NAME_AMERICANCATEGORY);
        $data = new AmericanDefineData();
        $result = $data->getAll($americanDefineFilter);
        if($returnOne){
            return $result[0];
        }else{
            return $result;
        }
    }
    
    /**
     * 通过美剧ID获取信息(filter)
     */
    public static function getOneAmericanDefine($filter){
        $data = new AmericanDefineData();
        return $data->getOne($filter);
    }
    
    
    /**
     * 删除美剧
     */
    public static function changeAmericanStatus($americanId,$status){
        self::checkOperation(self::FUNC_CODE_AMERICAN_DELETE);
        $data = new AmericanDefineData();
        $data->changeAmericanStatus($americanId,$status);
        self::saveOperateHistory(self::FUNC_CODE_AMERICAN_DELETE, $status);
    }
    
    
    /**
     * 获取周播表信息
     */
    public static function getAmericanWeekPlay($filter){
        $data = new AmericanDefineData();
        return $data->getAll($filter);
    }
    
    public static function getAmericanByFilter($filter,$isGetOne = true){
        $americanData = new AmericanDefineData();
        if($isGetOne){
            $data = $americanData->getOne($filter);
        }else{
            $data = $americanData->getAll($filter);
        }
        return $data;
    }
    
    /**
     * 通过周播剧集ID获取信息
     */
    public static function getAmericanWeekPlayById($episodesId){
        $data = new AmericanDefineData();
        return $data->getAmericanWeekPlayById($episodesId);
    }
    
    /**
     * 获取本周手动周播表
     */
    public static function getWeekPlayAutoData(){
        $data = new AmericanDefineData();
        return $data->getWeekPlayAutoData();
    }
    
    /**
     * 通过ID编辑周播剧集信息
     */
    public static function updateAmericanWeekPlayById($weekId,$americanId,$weekDate,$episodesNum){
        $data = new AmericanDefineData();
        return $data->updateAmericanWeekPlayById($weekId,$americanId,$weekDate,$episodesNum);
    }
    
    /**
     * 删除美剧周播记录
     */
    public static function delAmericanWeekPlay($weekId){
        self::checkOperation(self::FUNC_CODE_AMERICAN_WEEKDELETE);
        $data = new AmericanDefineData();
        $data->delAmericanWeekPlay($weekId);
        self::saveOperateHistory(self::FUNC_CODE_AMERICAN_WEEKDELETE, $weekId);
    }
    

    
    /**
     * 添加美剧
     */
    public static function saveAmericanInfo($model,$articleTag,$americanRadio,$americanType,$mergeArr,$tagModel){
        self::checkOperation(self::FUNC_CODE_AMERICAN_ADD);
        if(!$model->NameCH){
            throw new BusinessException('中文名不能为空');
        }
        if(!$model->NameEN){
            throw new BusinessException('英文名不能为空');
        }
        if(!$model->AvatarUrl){
            throw new BusinessException('头像不能为空');
        }
        if(empty($americanRadio)){
            throw new BusinessException('所属电台不能为空');
        }
        if(empty($americanType)){
            throw new BusinessException('剧目类别不能为空');
        }
        if(empty($mergeArr)){
            throw new BusinessException('导演/主演不能为空');
        }
        if(!$model->Content){
            throw new BusinessException('介绍不能为空');
        }
        if(!$model->Id){
            $chineseSpell = new ChineseSpell();
            $model->PinyinCH = strtolower(substr($chineseSpell->getFullSpell($model->NameCH," "),0,1));
            $model->PinyinEN = strtoupper(substr($model->NameEN,0,1));
            $model->CreateBy = self::getCurrentUser()->Id;
            $model->CreateTime  = new \DateTime();
            $model->UpdateTime = new \DateTime();
            
            
            if($tagModel->TagName){
                $data = new TagData();
                $tagExistInfo = \haibao\cms\business\Tag::getTagIdByName($tagModel->TagName);
                if($tagExistInfo){
                    throw new BusinessException('该标签已存在');
                }
                $tagModel->Creator = $tagModel->Suser = self::getCurrentUser()->Id;
                $tagModel->TopId = 11280;
                $tagModel->CreateTime = $tagModel->LastModified = time();
                $tagModel->TopicId = (int)($data->getMaxTopicId() + 1);
                $tagInfo = \haibao\cms\business\Tag::getTagById($tagModel->ParentId);
                if(isset($tagInfo->TagLevel) && $tagInfo->TagLevel){
                    $tagModel->TagLevel = $tagInfo->TagLevel + 1;
                }else{
                    $tagModel->TagLevel = 2;
                }
            }else{
                throw new BusinessException('请填写所属上级标签');
            }
           
        }else{
            $americanInfo = self::getAmericanDefineInfoById($model->Id);
            $model->CreateBy = $americanInfo->CreateBy;
            $model->PinyinCH = $americanInfo->PinyinCH;
            $model->PinyinEN = $americanInfo->PinyinEN;
            $model->CreateTime = $americanInfo->CreateTime;
            $model->UpdateTime = date('Y-m-d H:i:s');
        }
        $model->IndexTime= new \DateTime();
        $americanData = new AmericanDefineData();
        $americanData->saveAmericanInfo($model,$articleTag,$americanRadio,$americanType,$mergeArr,$tagModel);
        self::saveOperateHistory(self::FUNC_CODE_AMERICAN_ADD, $model->toArray(true,true));
    }
    
    /**
     * 添加周播剧集信息
     */
    public static function addWeekInfo($americanPlayWeekModel){
        if(!$americanPlayWeekModel->AmericanId && (!$americanPlayWeekModel->AmericanNameCH || !$americanPlayWeekModel->AmericanNameEN)){
            throw new BusinessException('搜索与手动必填一处');
        }
        if(!$americanPlayWeekModel->PlayDate){
            throw new BusinessException('时间不能为空');
        }
        self::checkOperation(self::FUNC_CODE_AMERICAN_WEEKADD);
        $americanData = new AmericanDefineData();
        $americanData->addWeekInfo($americanPlayWeekModel);
        self::saveOperateHistory(self::FUNC_CODE_AMERICAN_WEEKADD, $americanPlayWeekModel->toArray(true,true));
    }

    /**
     * @param $starTagIds   美剧明星标签Id
     * @param $tvTagIds     美剧标签Id
     * @param $articleId    要排除的文章Id
     * @param $isTvStarTag  是否是剧星动态的标识
     * @return array
     */
    public static function getTvStar($starTagIds,$tvTagIds,$articleId,$isTvStarTag){
        $notInArr = array($articleId);
        $americanData = new AmericanDefineData();
        $stars = $americanData->getTvStarByStarTagIds($starTagIds);
        $celebrityInfo = $americanDefineInfo = $showAmericans = $tvIds = array();
        $tvArticleTagId = 0;

        $tvTagNames = \haibao\cms\business\Tag::getTagsById($tvTagIds);
        if($tvTagNames){
            foreach($tvTagNames as $key=>$value){
                $americanDefineId = $americanData->getAmericanDefineByName($value['TagName']);
                if($americanDefineId){
                    $tvIds[$americanDefineId['Id']] = $key;
                }
            }
        }
        if($tvIds){
            $americanDefineInfo = self::getAmericanDefineInfoById(array_keys($tvIds));
            foreach($americanDefineInfo as $info){
                if($info->Published != \haibao\cms\model\data\americandrama\AmericanDefine::PUBLISHED_STATUS_DISABLE){
                    $showAmericans = $info;
                    $tvArticleTagId = $tvIds[$info->Id];
                    $americanStars = $showAmericans->AmericanStars;
                    foreach($americanStars as $star){
                        if($star->TagId &&  $star->TagType == 1 && $star->StarType == \haibao\cms\model\data\americandrama\AmericanStars::STARTYPE_MAIN_ACTOR) {
                            $stars[] = $star->TagId;
                        }
                    }
                    break;
                }
            }
        }

        $stars = array_slice($stars,0,2);
        if($stars){
            $celebrityInfo = \haibao\cms\business\Celebrity::getCelebrityProInfoByTagIds($stars);
        }

        $tvArticles = $starArticles = $readArticles = array();

        if($isTvStarTag==\haibao\cms\model\data\Tag::TV_STAR_TREND_TAG_ID && $stars){
            $starArticles = \haibao\cms\business\Article::getArticleByTagId($stars[0],$notInArr,3);
            if(count($starArticles) < 3 && $tvArticleTagId){
                $tvArticles = \haibao\cms\business\Article::getArticleByTagId($tvArticleTagId,self::getNotInArr($starArticles,$notInArr),3);
            }

        }else{
            if($tvArticleTagId){
                $tvArticles = \haibao\cms\business\Article::getArticleByTagId($tvArticleTagId,$notInArr,3);
            }
            if(count($tvArticles) < 3 && $stars){
                $starArticles = \haibao\cms\business\Article::getArticleByTagId($stars[0],self::getNotInArr($tvArticles,$notInArr),3);
            }
        }

        $readArticles = array_merge($starArticles,$tvArticles);
        if(count($readArticles) < 3){
            $moreArticles =  \haibao\cms\business\Article::getArticleByTagId($isTvStarTag,self::getNotInArr($readArticles,$notInArr),3);
            $readArticles = array_merge($readArticles,$moreArticles);
        }
        $readArticles = array_slice($readArticles,0,3);
        return array($celebrityInfo,$showAmericans,$readArticles);
    }

    public static function getRecDrama($cateIds,$dramaId){
        $dramas = array();
        if($cateIds){
            $americanData = new AmericanDefineData();
            $dramas = $americanData->getRecDrama($cateIds,$dramaId);
        }
        return $dramas;
    }

    public static function updatePublishStatus($americanId,$status){
        $americanData = new AmericanDefineData();
        $americanData->updatePublishStatus($americanId,$status);
    }

    public static function updateAmericanInfo($articleTagModel){
        if(!empty($articleTagModel[0]->TagId)){
            foreach($articleTagModel as $key=>$val){
                $tagIds[] = $val->TagId;
            }
            if($tagIds){
                $americanData = new AmericanDefineData();
                $americanIds = $americanData->getAmericanIdByTagId($tagIds);
                if($americanIds){
                    $americanData->updateAmericanInfo($americanIds);
                }
            }
        }
    }
    public static function updateCommentNum($americanId,$num=1,$isSub=false){
    	if(!$americanId){
    		throw new \haibao\cms\business\BusinessException('Id不能为空');
    	}
    	$data = new AmericanDefineData();
    	$data->updateCommentNum((int)$americanId,$num,$isSub);
    }
    
    private static function getNotInArr($articles,$notInArr){
        $articleIds = array();
        foreach($articles as $article){
            $articleIds[] = $article['Id'];
        }
        return array_merge($notInArr,$articleIds);
    }
    public static function getAmericanProperty($arrIds){
        if(!$arrIds){
            throw new \haibao\cms\business\BusinessException('Id不能为空');
        }
        $data = new AmericanDefineData();
        return $data->getAmericanProperty($arrIds);
    }
} 